package com.cc.visitor.utils;

/**
 * @author chanchaw
 * @create 2024-09-23 9:18
 */
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.Map;

/**
 * @author chanchaw
 * @create 2024-09-02 8:58
 */
@Component
public class RestTemplateUtils {
    @Resource
    private RestTemplate restTemplate;
    private ObjectMapper mapper = null;
    public <T> T postJson(String url, String param, Class<T> responseType) throws JsonProcessingException {
        HttpHeaders headers = new HttpHeaders();
        MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
        headers.setContentType(type);
        headers.add("Accept", MediaType.APPLICATION_JSON.toString());

        mapper = new ObjectMapper();
        ObjectWriter ow = mapper.writer().withDefaultPrettyPrinter();
        String paramJsonString = param;

        HttpEntity<String> formEntity = new HttpEntity<>(paramJsonString, headers);
        String responseJsonString = restTemplate.postForObject(url, formEntity, String.class);

        return mapper.readValue(responseJsonString, responseType);
    }

    public <T> T postJson(String url, Map<String, Object> param, Class<T> responseType) throws JsonProcessingException {
        HttpHeaders headers = new HttpHeaders();
        MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
        headers.setContentType(type);
        headers.add("Accept", MediaType.APPLICATION_JSON.toString());

        mapper = new ObjectMapper();
        ObjectWriter ow = mapper.writer().withDefaultPrettyPrinter();
        String paramJsonString = ow.writeValueAsString(param);

        HttpEntity<String> formEntity = new HttpEntity<>(paramJsonString, headers);
        String responseJsonString = restTemplate.postForObject(url, formEntity, String.class);

        return mapper.readValue(responseJsonString, responseType);
    }

    /**
     * post方法发送 formData 数据的请求
     * @param url
     * @param param
     * @param responseType
     * @return
     * @param <T>
     * @throws JsonProcessingException
     */
    public <T> T postForm(String url, MultiValueMap<String, Object> param, Class<T> responseType) throws JsonProcessingException {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        HttpEntity<MultiValueMap<String, Object>> reqParam = new HttpEntity<>(param, headers);
        ResponseEntity<String> res = restTemplate.postForEntity(url, reqParam, String.class);
        String bodyString = res.getBody();
        mapper = new ObjectMapper();
        return mapper.readValue(bodyString, responseType);
    }

    public <T> T get4Object(String url,Class<T> resType){
        return restTemplate.getForObject(url, resType);
    }
}


